package com.lxy.mychat.ui.MyChat;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.lxy.mychat.Adapter.ChatItemAdapter;
import com.lxy.mychat.Bean.ChatItem;
import com.lxy.mychat.Bean.Msg;
import com.lxy.mychat.Bean.User;
import com.lxy.mychat.ChatActivity;
import com.lxy.mychat.R;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

public class MyChatFragment extends Fragment {
    private ListView chatitemlv;
    ChatItemAdapter adapter;
    List<ChatItem> chatItemList=new ArrayList<>();
    List<User> userList=new ArrayList<>();
    List<Msg> msgList=new ArrayList<>();
    int myid;
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_mychat, container, false);
        initData();
        initView(root);
        setListener();
        return root;
    }

    private void setListener() {
        chatitemlv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                User u=userList.get(position);
                Intent intent=new Intent(getContext(), ChatActivity.class);
                intent.putExtra("receivename",u.getName());
                intent.putExtra("receiveid",u.getId());
                startActivity(intent);
            }
        });
    }

    private void initData() {
        chatItemList.clear();
        myid=getActivity().getSharedPreferences("userinfo", Context.MODE_PRIVATE).getInt("userid",-1);
        userList= LitePal.where("id != ?",myid+"").find(User.class);
        Log.e("aaa",userList.size()+"");
        for (User u:userList){
            msgList=getChatContent(u);
            if (msgList.size()!=0){
                Msg lastmsg=msgList.get(msgList.size()-1);
                chatItemList.add(new ChatItem(u.getUserimg(),u.getName(),lastmsg.getContent(),lastmsg.getTime()));
            }
        }

    }

    private List<Msg> getChatContent(User u) {
        List<Msg> msgs=new ArrayList<>();
        for (Msg m: LitePal.findAll(Msg.class)){
            boolean a=myid==m.getReceive_id()&&u.getId()==m.getSend_id();
            boolean b=u.getId()==m.getReceive_id()&&myid==m.getSend_id();
            if (a||b){
                msgs.add(m);
            }
        }
        return msgs;
    }

    private void initView(View root) {
        chatitemlv=root.findViewById(R.id.chatitemlv);
        adapter=new ChatItemAdapter(getContext(),R.layout.chat_item,chatItemList);
        chatitemlv.setAdapter(adapter);
    }
    @Override
    public void onStart() {
        super.onStart();
        initData();
        adapter.notifyDataSetChanged();
    }
}